package me.zhengjie.modules.consultant.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.consultant.domain.Consultant;
import me.zhengjie.modules.consultant.service.ConsultantService;
import me.zhengjie.modules.consultant.service.dto.ConsultantDto;
import me.zhengjie.modules.consultant.service.dto.ConsultantQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author TianI
* @date 2022-01-29
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "置业顾问管理")
@RequestMapping("/api/consultant")
public class ConsultantController {

    private final ConsultantService consultantService;

    @GetMapping("/getConsultant/{id}")
    public ResponseEntity getConsultant (@PathVariable Long id){
        List<Consultant> consultant = consultantService.getConsultant(id);
        return new ResponseEntity<>(consultant,HttpStatus.OK);
    }

    @GetMapping
    @Log("查询置业顾问")
    @ApiOperation("查询置业顾问")
    @PreAuthorize("@el.check('consultant:list')")
    public ResponseEntity query(ConsultantQueryParam query, Pageable pageable){
        return new ResponseEntity<>(consultantService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增置业顾问")
    @ApiOperation("新增置业顾问")
    @PreAuthorize("@el.check('consultant:add')")
    public ResponseEntity create(@Validated @RequestBody ConsultantDto resources){
        return new ResponseEntity<>(consultantService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改置业顾问")
    @ApiOperation("修改置业顾问")
    @PreAuthorize("@el.check('consultant:edit')")
    public ResponseEntity update(@Validated @RequestBody ConsultantDto resources){
        consultantService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除置业顾问")
    @ApiOperation("删除置业顾问")
    @PreAuthorize("@el.check('consultant:del')")
    public ResponseEntity delete(@RequestBody Set<Long> ids) {
        consultantService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出置业顾问")
    @ApiOperation("导出置业顾问")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('consultant:list')")
    public void download(HttpServletResponse response, ConsultantQueryParam query) throws IOException {
        consultantService.download(consultantService.queryAll(query), response);
    }*/

}
